package com.onlinelibrary.activities.position;

import android.annotation.SuppressLint;
import android.app.Application;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.databinding.BindingAdapter;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.bumptech.glide.Glide;
import com.onlinelibrary.R;
import com.onlinelibrary.activities.App;
import com.onlinelibrary.databinding.ActivityPositionBinding;
import com.onlinelibrary.utils.Http;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class PositionVM extends AndroidViewModel {
    public static MutableLiveData<String>
            title=new MutableLiveData<>(),
            introduce=new MutableLiveData<>(),
            visitCount=new MutableLiveData<>(),
            img=new MutableLiveData<>(),
            videoUrl=new MutableLiveData<>();
    @SuppressLint("StaticFieldLeak")
    private static PositionActivity positionActivity;
    public static MutableLiveData<Boolean> isCollect=new MutableLiveData<>();
    private static String pos;
    public PositionVM(@NonNull Application application) {
        super(application);
        title.setValue("");
        introduce.setValue("");
        visitCount.setValue("");
        img.setValue("");
        videoUrl.setValue("");
        isCollect.setValue(false);
    }
    public void setBinding(ActivityPositionBinding binding, PositionActivity positionActivity, String pos) {
        PositionVM.positionActivity=positionActivity;
        PositionVM.pos=pos;
        title.postValue(pos);
        Http.get("position/map/?type=getInfo&pos=" + pos, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) { }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                try {
                    JSONObject jo = new JSONObject(response.body().string()).getJSONObject("data");
                    introduce.postValue(jo.getString("describe"));
                    img.postValue(Http.BASE_URL+ jo.getString("image"));
                    videoUrl.postValue(jo.getString("video"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        Http.get("visit/?type=get_visit&position=" + pos, new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) { }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) {
                try {
                    visitCount.postValue("已被访问"+response.body().string()+"次");
                } catch (Exception e) {
                    visitCount.postValue("已被访问0次");
                }
            }
        });
       if (App.hasLogin){
           refreshCollect();
       }else {
           App.autoLoginCheck(new Callback() {
               @Override
               public void onFailure(Call call, IOException e) {
               }
               @Override
               public void onResponse(Call call, Response response) throws IOException {
                   App.hasLogin=true;refreshCollect();
               }
           });
       }
    }
    private static void refreshCollect() {
        Http.get("collect/get/?type=getByEmailAndPos&posName="+ pos +"&email=" + App.sp.getString("account",""), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) { }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.body().string().equals("不存在")){
                    isCollect.postValue(false);
                }else{
                    isCollect.postValue(true);
                }
            }
        });
    }
    @BindingAdapter("bindImage")
    public static void bindImage(ImageView imageView,MutableLiveData<String> img){
        if (img.getValue()!=null&&!img.getValue().equals("")){
            if (img.getValue().equals("GONE")){
                imageView.setVisibility(View.GONE);
            }else{
                Log.i("emmmmmm", "bindImage: "+img.getValue());
                if (positionActivity!=null) positionActivity.runOnUiThread(() -> Glide.with(positionActivity).load(img.getValue()).into(imageView));
            }
        }
    }
    @BindingAdapter("bindVideo")
    public static void bindVideo(VideoView videoView,MutableLiveData<String> videoUrl){
       if (!videoUrl.getValue().equals("")){
           videoView.setVideoPath(Http.BASE_URL+videoUrl.getValue());
           videoView.start();
           videoView.setMediaController(new MediaController(positionActivity));
       }else{
           videoView.setVideoPath("https://desk.duba.com/yuanqi/images/lol/videoImages/vm2.mp4");
           videoView.start();
           videoView.setMediaController(new MediaController(positionActivity));
       }
    }
    public static void collect(View view){
        String way;
        if (isCollect.getValue()){
            way="delete";
        }else {
            way="add";
        }
        Http.get("collect/"+way+"/?posName="+ pos +"&email=" + App.sp.getString("account",""), new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) { }
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                if (response.body().string().equals("添加完毕")){
                    isCollect.postValue(true);
                }else{
                    isCollect.postValue(false);
                }
            }
        });
    }
    @BindingAdapter("bindCollect")
    public static void bindCollect(ImageView imageView,MutableLiveData<Boolean> isCollected){
        if (isCollected.getValue()){
            imageView.setImageResource(R.drawable.uncollect);
        }else {
            imageView.setImageResource(R.drawable.collect);
        }
    }
}